<?php
namespace App\Jobs;

use App\Models\Collecter\MiniCollecterCollected;
use App\Wen\Utils\CollecterUtils;
use App\Wen\Utils\ImageUtils;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class CollecterMediaProcessJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    public $tries = 1;
    protected $collected_id = 0;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($collected_id)
    {
        //
        $this->collected_id = $collected_id;
    }
    /**
     * 确定任务应该超时的时间
     *
     * @return \DateTime
     */
    public function retryUntil()
    {
        return now()->addMinutes(30);
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        if(_empty_($this->collected_id)){
            return;
        }
        $MiniCollecterCollected = MiniCollecterCollected::find($this->collected_id);
        if($MiniCollecterCollected && in_array($MiniCollecterCollected->status, [0, 4])){
            try {
                MiniCollecterCollected::where('id', $this->collected_id)->update([
                    'status' => 3
                ]);

                $content = CollecterUtils::content_media_process( $MiniCollecterCollected->content, 1);

                // 处理评论
                $comments = json_decode($MiniCollecterCollected->comments, true);
                $comments_ = [];
                if($comments){
                    foreach ($comments as $comment){

                        $comment['comment'] = CollecterUtils::content_media_process($comment['comment'], 1);

                        $img_url = '';
                        $comment['comment'] = preg_replace_callback("/<img.*?src[^\'\"]+[\'\"]([^\"\']+)[^>]+>/is", function ($r) use (&$img_url){
                            $url = CollecterUtils::imgurl_filter( CollecterUtils::repair_url( $r[1] ));
                            if(!_empty_($url)){
                                $img_url = $url;
                            }
                            return '';
                        }, $comment['comment']);

                        if($img_url && _empty_($comment['img'])){
                            $comment['img'] = $img_url;
                        }

                        if($comment['img']){
                            $process_url = ImageUtils::upload_url_img($comment['img'], '', ['w'=>0, 'h'=>-30], [], true, 1, 1, 3);
                            if($process_url && _array_key($process_url, 'url', '')){
                                $comment['img'] = $process_url['url'];
                            }
                        }


                        if(_array_key($comment, 'subcomments', [])){
                            foreach ($comment['subcomments'] as $subcomment){
                                $subcomment['comment'] = CollecterUtils::content_media_process($subcomment['comment'], 1);

                                $sub_img_url = '';
                                $subcomment['comment'] = preg_replace_callback("/<img.*?src[^\'\"]+[\'\"]([^\"\']+)[^>]+>/is", function ($r) use (&$sub_img_url){
                                    $url = CollecterUtils::imgurl_filter( CollecterUtils::repair_url( $r[1] ));
                                    if(!_empty_($url)){
                                        $sub_img_url = $url;
                                    }
                                    return '';
                                }, $subcomment['comment']);

                                if($sub_img_url && _empty_($subcomment['img'])){
                                    $subcomment['img'] = $sub_img_url;
                                }
                                if($subcomment['img']){
                                    $process_url = ImageUtils::upload_url_img($subcomment['img'], '', [], [], true, 1, 1, 3);
                                    if($process_url && _array_key($process_url, 'url', '')){
                                        $subcomment['img'] = $process_url['url'];
                                    }
                                }
                            }
                        }
                        $comments_[] = $comment;
                    }
                }
                $MiniCollecterCollected->content = $content;

                // 处理笔记图片
                $word_imgs = json_decode($MiniCollecterCollected->imgs, true);
                if($word_imgs && count($word_imgs) > 0){
                    $word_imgs =  CollecterUtils::download_img_arr($word_imgs, 1, 3);
                    $MiniCollecterCollected->imgs = json_encode($word_imgs);
                    $MiniCollecterCollected->content = strip_tags($MiniCollecterCollected->content);
                }


                $MiniCollecterCollected->comments = $comments_ ? json_encode($comments_) : '[]';
                $MiniCollecterCollected->status = 1;
                $MiniCollecterCollected->save();
            }catch (\Exception $e){
                _logger_(__file__, __line__, $e->getMessage());
                MiniCollecterCollected::where('id', $this->collected_id)->update([
                    'status' => 4
                ]);
            }
        }
    }
}
